package com.software.loginregister.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.software.loginregister.R;
import com.software.loginregister.activity.RecommendDetailActivity;
import com.software.loginregister.entity.vo.WorksSearchListVo;

import java.util.List;

public class SearchRecommendAdapter extends RecyclerView.Adapter<SearchRecommendAdapter.RecipeViewHolder> {
    private Context context;
    private List<WorksSearchListVo> worksList;

    public SearchRecommendAdapter(Context context, List<WorksSearchListVo> worksList) {
        this.context = context;
        this.worksList = worksList;
    }

    @NonNull
    @Override
    public RecipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recommend, parent, false);
        return new RecipeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeViewHolder holder, int position) {
        WorksSearchListVo works = worksList.get(position);

        // 设置数据
        holder.tvWorkName.setText(works.getWorksName());
        holder.tvUserName.setText(works.getNickname());
        holder.tvDifficulty.setText(works.getDifficulty());
        holder.tvMainIngredients.setText(works.getIngredients());
        holder.tvLikeCount.setText(String.valueOf(works.getLikeCount()));

        // 使用Glide加载图片
        Glide.with(context)
                .load(works.getPhotoUrl())
                .placeholder(R.drawable.image_loading)
                .error(R.drawable.image_error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .into(holder.ivWork);

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, RecommendDetailActivity.class);
            Long worksId = Long.valueOf(works.getWorksId());
            intent.putExtra("worksId", worksId);
            intent.putExtra("worksName", works.getWorksName());
            intent.putExtra("nickname", works.getNickname());
            intent.putExtra("photoUrl", works.getPhotoUrl());
            intent.putExtra("difficulty", works.getDifficulty());
            intent.putExtra("likeCount", works.getLikeCount());
            intent.putExtra("avatar", works.getAvatar());
            intent.putExtra("description", works.getDescription());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return worksList != null ? worksList.size() : 0;
    }

    public void filterList(List<WorksSearchListVo> filteredList) {
        this.worksList = filteredList;
        notifyDataSetChanged();
    }

    static class RecipeViewHolder extends RecyclerView.ViewHolder {
        ImageView ivWork;
        TextView tvWorkName;
        TextView tvMainIngredients;
        TextView tvUserName;
        TextView tvDifficulty;
        TextView tvLikeCount;

        public RecipeViewHolder(@NonNull View itemView) {
            super(itemView);
            ivWork = itemView.findViewById(R.id.iv_work);
            tvWorkName = itemView.findViewById(R.id.tv_work_name);
            tvMainIngredients = itemView.findViewById(R.id.tv_main_ingredients);
            tvUserName = itemView.findViewById(R.id.tv_user_name);
            tvDifficulty = itemView.findViewById(R.id.tv_difficulty);
            tvLikeCount = itemView.findViewById(R.id.tv_like_count);
        }
    }
}